<?php
/**
 * Created by PhpStorm.
 * User: xiaomage
 * Date: 2020/12/8
 * Time: 10:48
 */

namespace common\middleware;


use app\system\logic\User;
use common\ApiReturn;
use common\security\JsonToken;
use think\Request;

class Authorize
{
    /**
     * @param Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        $authorize = $request->header('Authorize');
        if (empty($authorize)) {
            return ApiReturn::code(AUTHORIZE_NULL);
        }

        $result = JsonToken::instance()->verifyToken();
        if (false === $result) {
            return ApiReturn::code(AUTHORIZE_ILLEGAL);
        }

        if (!JsonToken::instance()->sign()) {
            return ApiReturn::code(AUTHORIZE_ERROR);
        }

        User::uuid(JsonToken::instance()->sign());

        return $next($request);
    }
}